//
//  SYBaseWebView.m
//  TermOfSectionB
//
//  Created by 韩震明 on 15/11/29.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "SYBaseWebView.h"

@interface SYBaseWebView ()<UIWebViewDelegate>

@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation SYBaseWebView
{
    UIActivityIndicatorView *acView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createNavigation];
    
    [self createWebView];
}

#pragma  makr - Navigation Setting

- (void)createNavigation
{
    self.navigationItem.title = self.titleForWeb;
//    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
//    // 设置导航栏标题颜色
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
//
//    // 自定义导航栏左侧按钮
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(-20, 0, 40, 40);
    [leftButton setImage:[UIImage imageNamed:@"nav_back@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    [leftButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
//    // 导航栏是否透明
//    self.navigationController.navigationBar.translucent = NO;
}

#pragma mark - Navigationbar ButtonAction

- (void)leftBarButtonClick:(UIBarButtonItem *)leftBarButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - WebView Setting
- (void)createWebView
{
    self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.webView];
    
    [self.webView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.webView.delegate  = self;
    self.webView.scalesPageToFit = YES;
    
    // 风火轮
    acView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32.0f, 32.0f)];
    acView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    
    acView.center = self.view.center;
    [self.view addSubview:acView];
    
    [self createWebData];
}

- (void)createWebData
{
    NSURL *url = [NSURL URLWithString:self.WebViewUrlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    [self.hud removeFromSuperViewOnHide];
}

#pragma mark - WebView Delegate Method

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [acView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [acView stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    // 忽略异步加载返回 -999
    if ([error code] == NSURLErrorCancelled) return;
    
    [ENAlert alertShowTitle:@"网络错误" message:@"连接失败" cancelButtonTitle:@"取消" otherButtonTitles:@"重新加载" block:^(NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self createWebData];
        }
    }];
}

@end
